﻿using Core;
using Core.Data;
using Core.Domain.Common;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Services.Common
{
    /// <summary>
    /// 规模服务实现类
    /// </summary>
    public partial class ScaleService : IScaleService
    {
        
        #region Fields

        private readonly IRepository<Scale> _scaleRepository;

        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        public ScaleService(IRepository<Scale> scaleRepository)
        {
            this._scaleRepository = scaleRepository;
        }

        #endregion

        #region Method

        /// <summary>
        /// 删除规模
        /// </summary>
        /// <param name="entity"></param>
        public virtual void DeleteScale(Scale entity)
        {
            if (entity == null)
                throw new ArgumentNullException("Scale");

            _scaleRepository.Delete(entity);
        }

        /// <summary>
        /// 新增规模
        /// </summary>
        /// <param name="entity"></param>
        public virtual void InsertScale(Scale entity)
        {
            if (entity == null)
                throw new ArgumentNullException("Scale");

            _scaleRepository.Insert(entity);
        }

        /// <summary>
        /// 更新规模
        /// </summary>
        /// <param name="entity"></param>
        public virtual void UpdateScale(Scale entity)
        {
            if (entity == null)
                throw new ArgumentNullException("Scale");

            _scaleRepository.Update(entity);
        }

        /// <summary>
        /// 根据Id获取规模
        /// </summary>
        /// <param name="scaleId"></param>
        /// <returns></returns>
        public virtual Scale GetScaleById(int scaleId)
        {
            if (scaleId == 0)
                return null;

            return _scaleRepository.GetById(scaleId);
        }

        /// <summary>
        /// 获取所有规模
        /// </summary>
        /// <param name="active"></param>
        /// <returns></returns>
        public virtual IList<Scale> GetAllScales(bool? active = true)
        {
            var query = _scaleRepository.Table;

            if (active.HasValue)
                query = query.Where(s => s.Active == active);

            query = query.OrderBy(s => s.DisplayOrder);
            return query.ToList();
        }

        /// <summary>
        /// 获取分页规模
        /// </summary>
        /// <param name="name"></param>
        /// <param name="active"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public Core.IPagedList<Scale> GetAllScales(string name = "", bool? active = true, int pageIndex = 0, int pageSize = int.MaxValue)
        {
            var query = _scaleRepository.Table;

            if (!String.IsNullOrWhiteSpace(name))
                query = query.Where(s => s.Name.Contains(name));

            if (active.HasValue)
                query = query.Where(s => s.Active == active);

            query = query.OrderBy(s => s.DisplayOrder);

            return new PagedList<Scale>(query, pageIndex, pageSize);
        }
        #endregion
    }
}
